<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>JavaScript unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="../lib/prototype.js" type="text/javascript"></script>
  <script src="../lib/unittest.js" type="text/javascript"></script>
  <script src="../src/should_receive.js" type="text/javascript"></script>

  <script src="../src/utilities.js" type="text/javascript"></script>
  
  <script src="../src/behavior.js" type="text/javascript"></script>
  <script src="../src/suggestion.js" type="text/javascript"></script>
  <link rel="stylesheet" href="../lib/unittest.css" type="text/css" />
  <style type="text/css" media="screen">
    #suggestions, #test_box {
      border: 2px solid blue;
      background: white;
      position: absolute;
    }
    .selected {
      background:yellow;
    }
    #textmetric__ {
      background: lime;
    }
  </style>
</head>
<body>
<div id="testlog"></div>
<script type="text/javascript">
// <![CDATA[
  Behavior.add({
   ".tags": [new ObserveTokens(), new ShowSuggestions()]
  })
  

  document.observe("dom:loaded", function() { Event.updateObserversFor(document.body) })
  Test.context("The Suggestion modules", {
    setup: function() {
      dataTable = [
        {name: "joe", count:12},
        {name: "bob", count:32},
        {name: "ted", count:2},
        {name: "mike", count:58}
      ]
      
      TestController = {
        tokenActive: function(e) {
          // console.log(e.name, e.memo)
        }, 
        tokenInactive: function(e) {
          // console.log("not active")
        }
      }
      $T(TestController)
    },
    teardown: function() {
      // Event.stopObservingAllSelectors()
    },
    "ObserveTokens should recognize proper tokens ": function() {
      TestController.shouldReceive("tokenActive").exactly(5).times().verifyingEachTime({
        1:  function(e) {
              e.memo.content.shouldEqual("abcde")
              e.memo.position.shouldEqual(0)
            },
        2:  function(e) {
              e.memo.content.shouldEqual("f")
              e.memo.position.shouldEqual(6)
            },
        3:  function(e) {
              e.memo.content.shouldEqual('"gh ijk')
              e.memo.position.shouldEqual(8)
            },
        4:  function(e){
              e.memo.content.shouldEqual('"gh ijk lmn"')
              e.memo.position.shouldEqual(8)
            },
        5:  function(e) {
              e.memo.content.shouldEqual('opqrst')
              e.memo.position.shouldEqual(21)
            }
      })
      TestController.shouldReceive("tokenInactive").atLeast(1).times()
      Behavior.add({
        ".email":new ObserveTokens(),
        ".email::token:active": TestController.tokenActive,
        ".email::token:inactive": TestController.tokenInactive
      })
      // Empty - should trigger nothing
      $("email").focus()
      $('tags').focus()
      $("email").value = "abcde"
      $('email').setCaretPosition(3)
      $('tags').focus()
      $("email").value = "abcde f"
      $('email').setCaretPosition(7)
      $('tags').focus()
      $("email").value = 'abcde f "gh ijk'
      $('email').setCaretPosition(10)
      $('tags').focus()
      $("email").value = 'abcde f "gh ijk lmn"'
      $('email').setCaretPosition(10)
      $('tags').focus()
      $("email").value = 'abcde f "gh ijk lmn" opqrst'
      $('email').setCaretPosition(22)
    },
    "Selection box should build tables that can be populated and select values": function() {
      function assertRowSelected(idx) {
        assert($('test_box').rows[idx].hasClassName('selected'))
      }
      function assertRowNotSelected(idx) {
        assert(!$('test_box').rows[idx].hasClassName('selected'))
      }
      
      sbox = new SelectionBox('test_box');
      sbox.setData(dataTable)
      sbox.show()
      this.assert($('test_box'))
      
      sbox.select(1)
      assertRowSelected(1)
      
      sbox.selectPrevious()
      assertRowNotSelected(1)
      assertRowSelected(0)
      
      sbox.selectPrevious()
      assertRowNotSelected(0)
      assertRowSelected(3)
      
      sbox.selectNext()
      assertRowNotSelected(3)
      assertRowSelected(0)
      
      sbox.selectNext()
      assertRowNotSelected(0)
      assertRowSelected(1)
    },
    "Data source should get data": function() {
      
    }
    
  })
// ]]>
</script>
<form action="validation_test.html" method="get" accept-charset="utf-8" autocomplete="off">
  <label for="name">Name:</label>
  <input type="text" name="name" value="" id="name"><br />
  <label for="email">Email:</label>
  <input type="text" name="email" value="" id="email" class="email"><br />
  <label for="tags">Tags:</label>
  <input type="text" name="tags" value="" id="tags" class="tags"><br />
  <ul class="errors">
  </ul>
  <p><input type="submit" value="Continue &rarr;"></p>
</form>
</script>
</body>
</html>